package com.fanbuchi.fukua.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;

import com.fanbuchi.fukua.R;
import com.fanbuchi.fukua.activity.base.BaseActivity;
import com.fanbuchi.fukua.api.Oauth2API;
import com.fanbuchi.fukua.api.ProfileAPI;
import com.fanbuchi.fukua.entity.OauthEntity;
import com.fanbuchi.fukua.entity.UserEntity;
import com.fanbuchi.fukua.fragment.StartedUpFragmentSingle;
import com.fanbuchi.fukua.log.AcsfLog;
import com.fanbuchi.fukua.logger.Log;
import com.fanbuchi.fukua.manager.OauthManager;
import com.fanbuchi.fukua.manager.UserManager;
import com.fanbuchi.fukua.net.BaseCallBack;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;


/**
 *@title 启动页面
 * @title 1 不同的模板不同的fragment样式
 * @title 2 数据来源于本地db和图片
 * @title 3 设置 - 查看启动故事
 */
public class StartedUpActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_started_up);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new StartedUpFragmentSingle())
                    .commit();
        }

        OauthEntity oauth= OauthManager.getOauthEntity();
        if (oauth!=null){
            SparseArray<String> _result= ProfileAPI.getAPI().profile(profileCallBack());
            if (_result!=null){
                display(_result.get(0));
            }
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_started_up, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            startActivity(HomeActivity.class);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    protected Callback refreshTokenCallBack() {
        Callback _callback = new BaseCallBack<OauthEntity>(OauthEntity.class) {
            @Override
            public void onResponseOk(OauthEntity body) {
                if (body==null|| TextUtils.isEmpty(body.getAccessToken())){
                    startActivity(LoginActivity.class);
                    return;
                }
                OauthManager.setOauthEntity(body);

            }
        };
        return _callback;
    }


    public Callback profileCallBack(){
        Callback _callback=  new BaseCallBack<UserEntity>(UserEntity.class) {
            @Override
            public void onResponseOk(UserEntity body) {
                if (body.getCode()==0){
                    UserManager.setUserEntity(body);
                }else{
                    OauthManager.setOauthEntity(null);
                }
            }
        };
        return _callback;
    }

}
